<?php

function restaurant_offer_upload_page(){
    drupal_add_js(drupal_get_path('module','restaurant').'/javascript/jquery-ui/js/jquery-ui-1.8.20.custom.min.js','file');
    drupal_add_js('jQuery(document).ready(function () { jQuery("#edit-startdate").datepicker({ dateFormat: "yy-mm-dd" }); });', 'inline');
    drupal_add_js('jQuery(document).ready(function () { jQuery("#edit-enddate").datepicker({ dateFormat: "yy-mm-dd" }); });', 'inline');
    return drupal_render(drupal_get_form('restaurant_offer_upload_form'));
}

function restaurant_offer_upload_form($form,$form_state){
    if (isset($form_state['build_info']['args'][0])){
        $offer = ($form_state['build_info']['args'][0]);
    }else $offer=false;
    $restaurants = restaurant_get_restaurants_names();//lib mappában található
    $form['restaurant'] = array(
        '#type' => 'select',
        '#title' => t('Éttermek listája:'),
        '#options'=> $restaurants,
    );  
    $form['offer'] = array(
        '#type' => 'textarea',
        '#title' => t('Offer:'),
        "#default_value"=>$offer?$offer->offer:''
    );
    $form['price'] = array(
        '#type' => 'textfield',
        '#title' => t('Price:'),
         '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$offer?$offer->price:''
    );
    $form['startdate'] = array(
        '#type' => 'textfield',
        '#title' => t('Start'),
         '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$offer?$offer->startdate:''
    );
    $form['enddate'] = array(
        '#type' => 'textfield',
        '#title' => t('End:'),
         '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$offer?$offer->enddate:''
    );
    $form['oid']=array(
        '#type' =>'hidden',
        "#default_value"=>$offer?$offer->oid:''
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
    );

    return $form;
}

function restaurant_offer_upload_form_validate($form, &$form_values){
    if (strlen($form_values["values"]["offer"])<1){
        form_set_error('price',t('Az ajánlat mező kitöltése kötelező!'));
    }
    if (strlen($form_values["values"]["price"])<1){
        form_set_error('price',t('Túl alacsony árat adott meg!'));
    }
    if (strlen($form_values["values"]["startdate"])<1){
        form_set_error('price',t('Nem adott meg dátumot!'));
    }
    if (strlen($form_values["values"]["enddate"])<1){
        form_set_error('price',t('Nem adott meg dátumot!'));
    }
}

function restaurant_save_offer($offer){
    if ($offer->oid==''){
        db_insert('ep_offers') 
            ->fields(array(
                'offer' => $offer->offer,
                'price' => $offer->price,
                'startdate' => $offer->startdate,
                'enddate' =>$offer->enddate,
                'rid'=>$offer->rid
            ))
            ->execute();        
    }else{
        db_update('ep_offers') 
            ->fields(array(
                'offer' => $offer->offer,
                'price' => $offer->price,
                'startdate' => $offer->startdate,
                'enddate' =>$offer->enddate,
                'rid'=>$offer->rid,
            ))
                ->condition('oid',$offer->oid,'=')
            ->execute();         
    }

}

function restaurant_offer_upload_form_submit($form, &$form_values){
       $offer = new stdClass();
       $offer->offer= $form_values['values']['offer'];
       $offer->price= $form_values['values']['price'];
       $offer->startdate= $form_values['values']['startdate'];
       $offer->enddate= $form_values['values']['enddate'];
       $offer->rid = $form_values['values']['restaurant'];
       $offer->oid = $form_values['values']['oid'];
       restaurant_save_offer($offer);
       drupal_goto('restaurant/offer_list');
}